
//UTILITIES
function elmtName(oEl){
var oTd=oEl.parentElement;
var oTr=oTd.parentElement;
if(oTr.cells[oTd.cellIndex-1].tagName=="TH")return oTr.cells[oTd.cellIndex-1].innerText+": ";
return oTr.parentElement.parentElement.rows[0].cells[oTd.cellIndex].innerText+": ";
}
//check barcode
function chkBarCode(zCode,bNoPmt){
var l=zCode.length;
if(!/^[0-9]+$/.test(zCode)){return weMsg("条码必须是数字! 请检验条码:"+zCode,false);}
switch(l){
case 13:
case 12:
case 8:
	break;
default:
	if(!bNoPmt)weMsg("条码长度不对! 请检验条码!");
	return false;
}
var iChk=zCode.charCodeAt(--l)-48;
var iCnt=0;
while(--l>=0){
	iCnt+=(zCode.charCodeAt(l)-48)*3;
	if(--l>=0)iCnt+=zCode.charCodeAt(l)-48;
}
iCnt=(10-iCnt%10)%10;
if(bNoPmt=="genChk")return iCnt.toString();
if(iCnt==iChk)return true;
if(!bNoPmt)weMsg("条码效验位错误! 请检验条码!"+iCnt);
return false;
}
//format deicimal
function fmtDecimal(oVal,iSize){
var iLen=oVal.length;
var i=oVal.indexOf(".");
if(i>=0){
	i=iLen-i-1;
	if(i>iSize){return oVal.substr(0,iLen+iSize-i);}
	i=iSize-i;
}
else{oVal+=".";i=iSize;}
while(i-->0){oVal+="0";}
return	oVal;
}
//check date
var aLeapMonth=[31,29,31,30,31,30,31,31,30,31,30,31];// Leap year of days in month
function dateadd(iDays){
var oDate=new Date();
oDate=new Date(oDate.valueOf()+iDays*86400000);//24*3600*1000
var i,zRtn=oDate.getFullYear().toString();
i=oDate.getMonth()+1;zRtn+=(i>9)?i.toString():"0"+i.toString();
i=oDate.getDate();zRtn+=(i>9)?i.toString():"0"+i.toString();
return zRtn;
}
function datediff(zDay){
var oDate=new Date(zDay.substr(0,4)+"/"+zDay.substr(4,2)+"/"+zDay.substr(6,2));
var iVal=parseInt(oDate.valueOf()/86400000-10591);
return iVal;
}
function getDaysInMonth(iYear,iMonth){
if(iMonth!=1){return aLeapMonth[iMonth];}
return	(((iYear%4==0)&&(iYear%100!=0))||(iYear%400==0))?29:28;
}
function chkDate(oEl){
var zDate=oEl.value;
if(!/^[0-9]{8}$/.test(zDate)){weMsg(elmtName(oEl)+"日期格式错误!<br>日期格式:yyyymmdd");return false;}
var y=parseInt(zDate.substr(0,4),10);
var m=parseInt(zDate.substr(4,2),10);
var d=parseInt(zDate.substr(6,2),10);
if((m<1)||(m>12)){weMsg(elmtName(oEl)+"非法的 "+m+" 月");return false;}
if((d<1)||(d>getDaysInMonth(y,--m))){weMsg(elmtName(oEl)+"非法的 "+d+" 日");return false;}
return	true;
}
function chkTime(oEl){
if(!/^(([01][0-9])|(2[0-3]))/.test(oEl.value)){return weMsg(elmtName(oEl)+"小时格式:00-23;HH:MM:SS",false);}
if(!/^[0-9][0-9]:[0-5][0-9]/.test(oEl.value)){return weMsg(elmtName(oEl)+"分钟格式:00-59;HH:MM:SS",false);}
if(/^[0-9][0-9]:[0-5][0-9]$/.test(oEl.value)){oEl.value=oEl.value+":00";return true;}
if(!/^[0-9][0-9]:[0-5][0-9]:[0-5][0-9]$/.test(oEl.value)){return weMsg(elmtName(oEl)+"秒钟格式:00-59;HH:MM:SS",false);}
return true;
}
function chkNo(oEl){
var	iMax=(oEl.maxLength<9)?oEl.maxLength:9;
if(oEl.value.length>iMax)return weMsg(elmtName(oEl)+"输入编号超过 "+iMax+" 位!",false);
if(/^[0-9]{0,9}$/.test(oEl.value))return true;
return weMsg(elmtName(oEl)+"必须输入正整数!<br>格式: 9",false);
}
function chkInt(oEl){
var	iMax=(oEl.maxLength<9)?oEl.maxLength:9;
var zVal=oEl.value;
if(zVal.length>iMax)return weMsg(elmtName(oEl)+"输入正整数超过 "+iMax+" 位!",false);
if(/^[1-9][0-9]*$/.test(zVal))return true;
if(/^0$/.test(zVal))return true;
return weMsg(elmtName(oEl)+"必须输入正整数!<br>格式: 9",false);
}
function chkSignInt(oEl){
var	iMax=(oEl.maxLength<9)?oEl.maxLength:9;
var zVal=oEl.value;
if(zVal.length>iMax)return weMsg(elmtName(oEl)+"输入整数超过 "+iMax+" 位!",false);
if(/^[-+]?[1-9][0-9]*$/.test(zVal))return true;
if(/^[-+]?0$/.test(zVal))return true;
return weMsg(elmtName(oEl)+"必须输入整数!<br>格式: [-+]9",false);
}
function chkMoney(oEl){
var	iMax=(oEl.maxLength<14)?oEl.maxLength:14;
var zVal=oEl.value;
if(zVal.length>iMax)return weMsg(elmtName(oEl)+"输入金额超过 "+iMax+" 位!",false);
if(/^[1-9][0-9]*$/.test(zVal))return true;
if(/^[1-9][0-9]*.[0-9]{1,2}$/.test(zVal))return true;
if(/^0.[0-9]{1,2}$/.test(zVal))return true;
if(/^0$/.test(zVal))return true;
return weMsg(elmtName(oEl)+"必须输入正的金额!<br>格式: 9[.99]",false);
}
function chkSignMoney(oEl){
var	iMax=(oEl.maxLength<14)?oEl.maxLength:14;
var zVal=oEl.value;
if(zVal.length>iMax)return weMsg(elmtName(oEl)+"输入金额超过 "+iMax+" 位!",false);
if(/^[-+]?[1-9][0-9]*$/.test(zVal))return true;
if(/^[-+]?[1-9][0-9]*.[0-9]{1,2}$/.test(zVal))return true;
if(/^[-+]?0.[0-9]{1,2}$/.test(zVal))return true;
if(/^[-+]?0$/.test(zVal))return true;
return weMsg(elmtName(oEl)+"必须输入金额!<br>格式: [-+]9[.99]",false);
}
function byteLength(zVal){
var iLen=0;
for(var i=0;i<zVal.length;i++){iLen+=(zVal.charCodeAt(i)<128)?1:2;}
return	iLen;
}
function chkStr(oEl){
var zVal=oEl.value;
if(oEl.maxLength>512)return weMsg(elmtName(oEl)+"没有指定 maxlength 属性!",false);
var iMax=oEl.maxLength;
if(zVal.length<1)return weMsg(elmtName(oEl)+"至少输入一个字符!",false);
if(byteLength(zVal)>iMax)return weMsg(elmtName(oEl)+"不可超过 "+iMax+" 个字符!",false);
return	true;
}
function chkRPri(oEl){
var zVal=oEl.value;
if(/^[1-9][0-9]*$/.test(zVal))return true;
if(/^[1-9][0-9]*.[0-9]{1,2}$/.test(zVal))return true;
if(/^0.[0-9]{1,2}$/.test(zVal))return true;
return weMsg(elmtName(oEl)+"必须输入零售单价!<br>格式: 9[.99]",false);
}
function chkTPri(oEl){
var zVal=oEl.value;
if(/^[1-9][0-9]*$/.test(zVal))return true;
if(/^[1-9][0-9]*.[0-9]{1,4}$/.test(zVal))return true;
if(/^0.[0-9]{1,4}$/.test(zVal))return true;
return weMsg(elmtName(oEl)+"必须输入含税单价!<br>格式: 9[.9999]",false);
}
function chkUPri(oEl){
var zVal=oEl.value;
if(/^[1-9][0-9]*$/.test(zVal))return true;
if(/^[1-9][0-9]*.[0-9]{1,6}$/.test(zVal))return true;
if(/^0.[0-9]{1,6}$/.test(zVal))return true;
return weMsg(elmtName(oEl)+"必须输入无税单价!<br>格式: 9[.999999]",false);
}
//Interface
//handler of form submit
function noSubmit(){return false;}
//handler of close button
function weClose(el){
var oEl=(el)?el:this;
var oFrm=(oEl.tagName=="FORM")?oEl:oEl.form;
var oWin=oFrm.fEdit.oWin;
oWin.event.srcElement.blur();
oWin.document.body.innerHTML="";
}
//methods of dialog button
function weDialog(el){
var oEl=(el)?el:this;
if(oEl.tagName=="FORM"){
	return oEl.fEdit.dialog(oEl["weDialog"]);
}
else{
	return oEl.form.fEdit.dialog(oEl);
}}
//methods of weAction button
function weAction(el){
var oEl=(el)?el:this;
if(oEl.tagName=="FORM"){
	return oEl.fEdit.action(oEl["weAction"]);
}
else{
	return oEl.form.fEdit.action(oEl);
}}
//event interface of keydown
function keyElmt()	{return	this.form.fEdit.keyElmt(this);}
//event inferface of blur
function blurElmt()	{return	this.form.fEdit.blurElmt(this);}
//event interface of focus
function focusElmt(){return	this.form.fEdit.focusElmt(this);}
//initialize the from
function initForm(oFrm){
if(!oFrm.fEdit){oFrm.fEdit=new fnEdit(oFrm);}
return	oFrm.fEdit.initForm();
}

var oLastEdit=null;
//元素类型.[可获得焦点][可选定][Must zReg][key down]
aElement=new Array();
aElement["text"]			=new Array(true, true, true, 1);
aElement["submit"]			=new Array(true, false,false,3);
aElement["reset"]			=new Array(true, false,false,3);
aElement["button"]			=new Array(true, false,false,3);
aElement["select"]			=new Array(true, false,false,2);
aElement["checkbox"]		=new Array(true, false,false,2);
aElement["radio"]			=new Array(true, false,false,2);
aElement["select-one"]		=new Array(true, false,false,2);
aElement["select-multiple"]	=new Array(true, false,false,2);
aElement["password"]		=new Array(true, true, true, 1);
aElement["textarea"]		=new Array(true, true, true, 1);
aElement["file"]			=new Array(true, true, true, 1);
aElement["hidden"]			=new Array(false,false,false,1);
//METHOD
//constructor
function fnEdit(oFrm){
this.oFst=null;
this.oLast=null;
this.oCurElmt=null;
this.oFrm=oFrm;
this.oWin=oFrm.document.parentWindow;
}
//active the element
fnEdit.prototype.goHere=function(oEl){
if(!(aElement[oEl.type][0])){return weMsg("不能停留元素类型:"+oEl.type,false);}
if(aElement[oEl.type][0])oEl.focus();
if(aElement[oEl.type][1])oEl.select();
this.oCurElmt=oEl;
return true;
}
//set one element read only
fnEdit.prototype.setReadOnly=function(oEl){
oEl.style.border=0;
oEl.style.backgroundColor="transparent";
oEl.readOnly=true;
return	oEl.bChk=true;
}

//event handler of keydown
fnEdit.prototype.keyElmt=function(oEl){
if(this.oWin.event.keyCode!=13)return true;
switch(aElement[oEl.type][3]){
case 1:
	oEl.value=oEl.value;
	if(!this.chkElmt(oEl)){this.goHere(this.oCurElmt);return false;}
	if(oEl.onchange)oEl.onchange(oEl);
	if(oEl.oNext)return this.goHere(oEl.oNext);
	return	(oEl.oNext)? this.goHere(oEl.oNext):this.goHere(this.oFst);
case 2:
	return	(oEl.oNext)? this.goHere(oEl.oNext):this.goHere(this.oFst);
default:
	if(oEl.onclick)return oEl.onclick();
}}
//event hander of blur
fnEdit.prototype.blurElmt=function(oEl){
if(this.oCurElmt!=oEl)return true;
if(oEl.value=="")return true;
if(this.chkElmt(oEl))return true;
this.goHere(oEl);
return false;
}
//event hander of focus
fnEdit.prototype.focusElmt=function(oEl){
if(this.oCurElmt.value=="")return this.goHere(oEl);
if(this.oCurElmt.bChk)return this.goHere(oEl);
if(this.chkElmt(this.oCurElmt))return this.goHere(oEl);
this.goHere(this.oCurElmt);
return false;
}
//event handler of weDialog or weAction button
fnEdit.prototype.okTask=function(zTxt){
var oWin=oLastEdit.oWin;
if(oWin.okQury)return oWin.okQury(zTxt);
if(/^<table res=1 /.test(zTxt)){
	if(oWin.okPost){if(!oWin.okPost(zTxt))return true;}
	return weMsg("成功");
}
if(/aRtn./.test(zTxt)){
	var aRtn=new Array();
	eval(zTxt);
	if(aRtn.err!="1")return	weRtn(aRtn);
	if(oWin.okPost){if(!oWin.okPost(aRtn))return true;}
	return weMsg("成功");
}
return	weMsg(zTxt);
}
//form POSTmsg or CGIparameters
fnEdit.prototype.formCGI=function(){
var oSpe=null,bVal,iVal,zCgi="",oEl;
for(var i=0;i<this.oFrm.length;i++){
 oEl=this.oFrm.elements[i];
 if(oSpe){
	if(oEl.name==oSpe.name){
	 if(oEl.type!=oSpe.type){return weMsg(oSpe.name+".类型发生改变!"+oSpe.type+"!="+oEl.type,false);}
	 if(oEl.checked){bVal=true;iVal|=oEl.value;}
	 continue;
	}
	if((oSpe.type=="radio")&&(!bVal)){goHere(oSpe);return weMsg("'"+oEl.name+"'没有选择!",false);}
	zCgi+="&"+oSpe.name+".New="+iVal;
	oSpe=null;
 }
 switch(oEl.type){
 case "radio":
 case "checkbox":
	oSpe=oEl;bVal=false;iVal=0;
	if(oEl.checked){bVal=true;iVal|=oEl.value;}
	continue;
 }
 if(i==0)
	zCgi=oEl.name+".New="+escape(oEl.value);
 else
	zCgi+="&"+oEl.name+".New="+escape(oEl.value);
 if(oEl.zOrg)zCgi+="&"+oEl.name+".Org="+escape(oEl.zOrg);
}
return	zCgi;
}
fnEdit.prototype.dialog=function(oEl){
if(!this.chkElmts())return false;
if(this.oWin.event)this.oWin.event.srcElement.blur();
oLastEdit=this;
if(this.oFrm.method.toUpperCase()=="POST")
	return	amSend(this.oFrm.action,this.okTask,this.formCGI());
else
	return	amSend(this.oFrm.action+"&"+this.formCGI(),this.okTask);
}
fnEdit.prototype.action=function(oEl){
if(!this.chkElmts())return false;
if(this.oWin.event)this.oWin.event.srcElement.blur();
if(oEl=="undefined")return weMsg("失败! 没有定义 weAction 按钮",false);
oLastEdit=this;
var zUrl;
switch(oEl.value){
case "新增":
	zUrl=this.oFrm.action.replace(/Done./,"Insert.");break;
case "修改":
	zUrl=this.oFrm.action.replace(/Done./,"Update.");break;
case "删除":
	if(!confirm("信息删除后不可恢复！执行？"))return false;
	zUrl=this.oFrm.action.replace(/Done./,"Delete.");break;
default:
	zUrl=this.oFrm.action.replace(/Done./,"Confirm.");break;
}
if(this.oFrm.method.toUpperCase()=="POST")
	return	amSend(zUrl,this.okTask,this.formCGI());
else
	return	amSend(zUrl+"&"+this.formCGI(),this.okTask);
}
//initialize one element
fnEdit.prototype.setFst=function(oEl){
if(oEl.oFst){this.oFst=oEl;return true;}
if(this.oFst==null){this.oFst=oEl;}
return true;
}
fnEdit.prototype.setNext=function(oEl){
if(this.oLast){this.oLast.oNext=oEl;}
if(typeof(oEl.oNext)!="undefined"){this.oLast=null;return true;}
if(typeof(oEl.zNext)!="undefined"){
	if(this.oFrm[oEl.zNext])oEl.oNext=this.oFrm[oEl.zNext];
	else{return weMsg(oEl.name+"."+oEl.zNext+"不存在",false);}
	this.oLast=null;
	return true;
}
this.oLast=oEl;
return true;
}
fnEdit.prototype.initElmt=function(oEl){
if(oEl.type=="hidden")return true;
if(oEl.zOrg){oEl.zOrg=oEl.value;}
if(oEl.readOnly==true)return this.setReadOnly(oEl);
switch(oEl.type){
case "submit":
case "reset":
case "button":
 oEl.bChk=true;
 this.setNext(oEl);
 if(!oEl.className)oEl.className="button";
 switch(oEl.name){
 case "weDialog":
	if(!oEl.onfocus){oEl.onfocus=weDialog;}
	return true;
 case "weAction":
	if(!oEl.onfocus){oEl.onfocus=weAction;}
	return true;
 case "weClose":
	if(!oEl.onfocus){oEl.onfocus=weClose;}
	return true;
 default:
	return true;
}
case "select":
case "checkbox":
case "radio":
case "select-one":
case "select-multiple":
	oEl.bChk=true;
	if(!oEl.onblur)oEl.onblur=blurElmt;
	if(!oEl.onkeydown)oEl.onkeydown=keyElmt;
	if(!oEl.onfocus)oEl.onfocus=focusElmt;
	this.setFst(oEl);
	return	this.setNext(oEl);
default:
	if(!oEl.zReg){return this.setReadOnly(oEl);}
	oEl.bChk=false;oEl.style.backgroundColor="#FFFFFF";
	if(!oEl.onblur)oEl.onblur=blurElmt;
	if(!oEl.onkeydown)oEl.onkeydown=keyElmt;
	if(!oEl.onfocus)oEl.onfocus=focusElmt;
	this.setFst(oEl);
	return	this.setNext(oEl);
}}
//initialize the form
fnEdit.prototype.initForm=function(){
if(!this.oFrm)return weMsg("失败! 没有指定 FORM",false);
if(!this.oWin)return weMsg("失败! 没有指定 window",false);
this.oFrm.onsubmit=noSubmit;
for(var i=0;i<this.oFrm.length;i++){
	if(this.initElmt(this.oFrm.elements[i]))continue;
	return weMsg("初始化失败! Element="+this.oFrm.elements[i].name,false);
}
if(this.oFst)return this.goHere(this.oFst);
return weMsg("初始化失败! 没有输入元素 FORM="+this.oFrm.name,false);
}
//check one element
fnEdit.prototype.chkElmt=function(oEl){
if(!aElement[oEl.type][2])return true;
oEl.bChk=false;
if(oEl.vChk){
	switch(eval("this.oWin."+oEl.vChk+"(oEl)")){
	case 0:return false;
	case 1:oEl.value=oEl.value;return	oEl.bChk=true;
	default:break;
}}
if(!oEl.zReg){return weMsg(elmtName(oEl)+"没有 oReg 表达式",false);}
//if(!oEl.oNext){return weMsg(elmtName(oEl)+"没有 oNext 对象",false);}
if(oEl.BarLead){
	if(!oEl.BarMatch){return weMsg(elmtName(oEl)+"没有条码截取控制",false);}
	var oReg=RegExp(oEl.BarLead);
	if(oReg.test(oEl.value)){if(!chkBarCode(oEl.value)){return false;}oEl.value=eval("oEl.value."+oEl.BarMatch);}
}
var zReg;
if(/Null$/.test(oEl.zReg)){
	if(oEl.value.length==0){oEl.value=oEl.value;return	oEl.bChk=true;}
	zReg=oEl.zReg.replace(/Null/,"");
}
else zReg=oEl.zReg;
switch(zReg){
case "noChk"://no check need
	break;
case "date":
	if(!chkDate(oEl))return false;
	break;
case "time"://must be time
	if(!chkTime(oEl))return false;
	break;
case "num":
	if(!chkNo(oEl))return false;
	break;
case "int":
	if(!chkInt(oEl))return false;
	break;
case "signInt":
	if(!chkSignInt(oEl))return false;
	break;
case "money":
case "Amt":
	if(!chkMoney(oEl))return false;
	break;
case "signMoney":
	if(!chkSignMoney(oEl))return false;
	break;
case "str":
	if(!chkStr(oEl))return false;
	break;
case "RPri":
	if(!chkRPri(oEl))return false;
	break;
case "TPri":
	if(!chkTPri(oEl))return false;
	break;
case "UPri":
	if(!chkUPri(oEl))return false;
	break;
case "barCode":
	if(!chkBarCode(oEl.value))return false;
	break;
case "barLead":	//must be bar code with leading code
	if(!oEl.barLead){return weMsg(zName+"没有条码标识",false);}
	if(!chkBarCode(oEl.value))return false;
	var oReg=RegExp("^"+oEl.barLead+"[0-9]+");
	if(!oReg.test(oEl.value)){return weMsg(zName+"条码标识不是:"+oEl.barLead,false);}
	break;
default:
	var oReg=RegExp(oEl.zReg);
	if(oReg.test(oEl.value))break;
	switch(oEl.zReg){
	case	".+":	return weMsg(elmtName(oEl)+'至少输入一个字符',false);
	default:		return weMsg(elmtName(oEl)+'输入数据类型或格式错误!<br>Reg="'+oReg+"'",false);
	}
}
oEl.value=oEl.value;
return	oEl.bChk=true;
}
//check all elements in the form
fnEdit.prototype.chkElmts=function(){
for(var i=0;i<this.oFrm.length;i++){
	oEl=this.oFrm.elements[i];
	if(!oEl.zReg && !oEl.vChk)continue;
	if(oEl.readOnly && !oEl.vChk)continue;
	if(this.chkElmt(oEl))continue;
	this.goHere(oEl);
	return false;
}//end of for
return	true;
}
